<h1 class="title-h1">公告列表</h1>
<div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="app-content-list">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-inline">
                <input type="text" name="name" placeholder="请输入姓名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layuiadmin-btn-list" lay-submit="" lay-filter="search">
                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i> 搜索
            </button>
        </div>
    </div>
</div>

<div class="layui-card-body">
    <table class="layui-table" id="dataTable" lay-filter="dataTable">
    </table>
    <script type="text/html" id="dataTableToolbar">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        </div>
    </script>
    <script type="text/html" id="dataTableBar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script type="text/html" id="topTpl">
        {{ d.is_top?'是':'' }}
    </script>

</div>
<script>
    layui.use(['table', 'http', 'form'], function () {
        var table = layui.table,
            layer = layui.layer,
            http = layui.http,
            $ = layui.$;
        var form = layui.form;
        form.on('submit(search)', function (data) {
            reloadTable(data.field)
            return false;
        });
        table.render({
            elem: '#dataTable',
            url: '/setting/noticelist/',
            toolbar: '#dataTableToolbar',
            page: true,
            limit: 20,
            limits: [20, 50, 100, 500],
            cols: [
                [{
                    field: 'title',
                    title: '标题',
                }, {
                    field: 'come_from',
                    title: '来源',
                }, {
                    field: 'content',
                    title: '内容',
                }, {
                    field: 'gang_name',
                    title: '帮会',
                }, {
                    field: 'is_top',
                    templet: '#topTpl',
                    title: '置顶',
                }, {
                    field: 'user_name',
                    title: '发布人',
                }, {
                    field: 'time',
                    title: '发布时间',
                }, {
                    title: '操作',
                    toolbar: '#dataTableBar',
                    minWidth: 150,
                    fixed: 'right'
                }]
            ],
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.message,
                    "count": res.data.count,
                    "data": res.data.data
                };
            }
        });

        table.on('toolbar(dataTable)', function (obj) {
            // var checkStatus = table.checkStatus(obj.config.id);
            console.log(obj)
            switch (obj.event) {
                case 'add':
                    editFunc('')
                    break;
            };
        });
        var reloadTable = function (where, sort) {
            table.reload("dataTable", {
                initSort: sort,
                where: where,
            });
        }
        var editFunc = function (id) {
            http.get('/setting/noticeedit?id=' + id, id ? '编辑公告' : '添加公告','750px'); 
        }
        //监听行工具事件
        table.on('tool(dataTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除这条数据吗?', {
                    skin: 'layui-layer-admin',
                }, function (index) {
                    console.log(index)
                    http.post('/setting/noticedel', {
                        id: data.id
                    }, function (res) {
                        obj.del();
                        layer.close(index);
                    })
                });
            } else if (obj.event === 'edit') {
                editFunc(data.id)
            }
        });
        table.on('sort(dataTable)', function (obj) {
            reloadTable({
                field: obj.field,
                order: obj.type
            }, obj)
        });
    });
</script>